package cn.niit.letuseatdemo.controller.store;

import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: zhang_bowen
 * @CreateTime： 2022/12/1 22:22
 */
@Slf4j
@Controller
public class StoreRegisterController {

    @Autowired
    private StoreService storeService;

    /*
      * @Description: 初始化页面
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/8  19:46
 * @return: java.lang.String
      **/
    @GetMapping("/store/register")
    public String saveregister(Model model,Store store){
        model.addAttribute("store",store);
        return "/store/register";
    }
    /*
      * @Description: 用于商家访问/store/register后在浏览器中显示入驻页面
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/1  23:15
 * @return: java.lang.String
      **/
    @PostMapping("/store/register")
    public String storeRegister(@Validated Store store, Model model, BindingResult result) {
        log.info("待注册的店铺信息：{}", store);
        try {
            Store storePhone = storeService.findStorePhone(store.getStorePhone());
            //没有用户可以进行注册
            if(storePhone == null){
                if(result.hasErrors()){//如果返回ture。说明参数校验不通过
                    //存放校验错误信息的集合
                    Map<String,String> errorsMap = new HashMap<>();
                    for (FieldError error: result.getFieldErrors()){
                        log.error("参数校验错误：{}，{}",error.getField(),error.getDefaultMessage());
                        errorsMap.put(error.getField(),error.getDefaultMessage());
                    }
                    model.addAttribute("errors",errorsMap);//将错误信息返回给前端
                    model.addAttribute("store",store);
                    return "/store/register";
                }else if(!store.getStorePassword().equals(store.getStorePassword1())){
                    model.addAttribute("tip","密码前后不一致");
                    return "/store/register";
                }else {
                    int i =storeService.insertAdminStore(store);
                    if (i>0){
                        model.addAttribute("tip","审核以提交，等待管理员审核通过后，即可登录");
                    }
                    return "/store/register";
                }
            }else {
                model.addAttribute("tip","用户已存在，请登录");
                return "/store/register";
            }
        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

}